﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIGCBI.Entities;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;

namespace SIGCBI.Repository
{
    public interface IFuenteFinancieraRepository
    {
        List<FUENTE_FINANCIERA> GetAllFromFuenteFinanciera();
        List<FUENTE_FINANCIERA> GetFromFuenteFinancieraByCriterio(string criterio);
        List<DETALLE_PARTIDA> GetFromDetalleFuenteFinancieraByIdFuente(int idFuente);
        FUENTE_FINANCIERA ValidarDuplicidadFuenteFinanciera(string codigo, int id);
        FUENTE_FINANCIERA GetFromFuenteFinancieraById(Int32 id);
        void AddFuenteFinanciera(FUENTE_FINANCIERA fuenteFinanciera);
        void UpdateFuenteFinanciera(FUENTE_FINANCIERA fuenteFinanciera);
        void DeleteFuenteFinanciera(int idFuenteFinanciera);
    }

    public class FuenteFinancieraRepository : MasterDataAccess , IFuenteFinancieraRepository
    {
        public List<FUENTE_FINANCIERA> GetAllFromFuenteFinanciera()
        {
            var query = from c in Context.FUENTE_FINANCIERAs.Include("CENTRO_COSTO")
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<FUENTE_FINANCIERA> GetFromFuenteFinancieraByCriterio(string criterio)
        {
            if (criterio != "")
            {
                var query = from c in Context.FUENTE_FINANCIERAs.Include("CENTRO_COSTO")
                            where c.Codigo.ToUpper().Contains(criterio.ToUpper()) ||
                            c.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.FUENTE_FINANCIERAs.Include("CENTRO_COSTO")
                             select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query1);

                return query1.ToList();
            }
        }

        public FUENTE_FINANCIERA ValidarDuplicidadFuenteFinanciera(string codigo, int id)
        {
            var query = from c in Context.FUENTE_FINANCIERAs.Include("CENTRO_COSTO")
                        where (c.Codigo.ToUpper().CompareTo(codigo.ToUpper()) == 0)
                        && c.Id != id
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.FirstOrDefault();
        }

        public FUENTE_FINANCIERA GetFromFuenteFinancieraById(int id)
        {
            var query = from p in Context.FUENTE_FINANCIERAs.Include("CENTRO_COSTO").Include("DETALLE_PARTIDA.PARTIDA")
                        where p.Id.Equals(id)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();  
        }

        public void AddFuenteFinanciera(FUENTE_FINANCIERA fuenteFinanciera)
        {
            Context.FUENTE_FINANCIERAs.Add(fuenteFinanciera);
            Context.SaveChanges();
        }

        public void UpdateFuenteFinanciera(FUENTE_FINANCIERA fuenteFinanciera)
        {
            fuenteFinanciera.DETALLE_PARTIDA = null;
            Context.FUENTE_FINANCIERAs.Attach(fuenteFinanciera);
            Context.Entry(fuenteFinanciera).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void DeleteFuenteFinanciera(int idFuenteFinanciera)
        {
            var existe = Context.FUENTE_FINANCIERAs.Find(idFuenteFinanciera);

            if (existe != null)
            {
                Context.FUENTE_FINANCIERAs.Remove(existe);
                Context.SaveChanges();
            }
        }


        public List<DETALLE_PARTIDA> GetFromDetalleFuenteFinancieraByIdFuente(int idFuente)
        {
            var query = from p in Context.DETALLE_PARTIDA.Include("PARTIDA")
                        where p.Id.Equals(idFuente)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }
    }
}
